//订单管理
var url = require('url');
var template = require('templateConfig');
var main = require('main');
var module = require('module');
var form = require('form');

//模板配置
var tplArr = [];
var ManagerTpl =require('raw!../template/manager.txt'); 
tplArr['manager'] = template.compile(ManagerTpl.replace(/^\s*|\s*$/g, ""));

//数据平铺
var data1 = {"list":[
				{"name":"累计销售额","key":"10"},
				{"name":"累计佣金笔数","key":"10"},
				{"name":"累计佣金额","key":"0"},
			]};

var goodsInfo = {
    "id": 1,
    "title":"大家乐文胸一片式无痕内衣厚B杯深V聚拢商场同款内衣1",
    "attr": [],
    "num":"1002837828372",
    "spec":"大红,B杯,34/75",
    //"quantity":"1",
    //"inStock":"258",
    //"colour":"红色、粉色、嫩肤",
    //"cup":"A杯、B杯、C杯",
    //"size":"32/70、40/75",
    //"price":"98",
};
var data2 = [
            	{
            		"id":"",
            		"num":"12321231",
            		"goodsInfo": module.ordersGoodsInfo([goodsInfo]),
            		"editUrl":"#",
            		"price":"99.10",
            	},{
            		"id":"",
            		"num":"12321232",
            		"goodsInfo": module.ordersGoodsInfo([goodsInfo]),
            		"editUrl":"#",
            		"price":"99.20",
            	},{
            		"id":"",
            		"num":"12321233",
            		"goodsInfo": module.ordersGoodsInfo([goodsInfo]),
            		"editUrl":"#",
            		"price":"99.30",
            	}
            ];
var data3 = [
            	{
            		"id":"",
            		"num":"12321231",
            		"goodsInfo": module.ordersGoodsInfo([goodsInfo]),
            		"editUrl":"#",
            		"price":"99.10",
            	},{
            		"id":"",
            		"num":"12321232",
            		"goodsInfo": module.ordersGoodsInfo([goodsInfo]),
            		"editUrl":"#",
            		"price":"99.20",
            	},{
            		"id":"",
            		"num":"12321233",
            		"goodsInfo": module.ordersGoodsInfo([goodsInfo]),
            		"editUrl":"#",
            		"price":"99.30",
            	}
            ];

// var data3 = [
//             	{
//             		"clerk_id":"",
//             		"clerk_name":""
//             		"num":"12321231",
//             		"editUrl":"#",
//             		"price":"99.10",
//             	},{
//             		"id":"",
//             		"clerk_name":""
//             		"num":"12321232",
//             		"editUrl":"#",
//             		"price":"99.20",
//             	},{
//             		"id":"",
//             		"clerk_name":""
//             		"num":"12321233",
//             		"editUrl":"#",
//             		"price":"99.30",
//             	}
//             ];



function Index(){
    _this = this;
    this.userStatus = '';
    this.main = function(){
        main.event(this,'tap','om-click'); 
        main.event(this,'change','om-change'); 
        this.init();
    }
    //初始化内容
    this.createHtml = function(){
    	console.log(_this.pageName);
    	var html = '';
    	switch(_this.pageName){
    		//订单管理
    		case "orderManager":
    			_this.getOrderData();
    		break;
    		//商品管理
    		case "productManager":
    			main.get(url.getStoreList,{"store_id":_this.store_id},function(ret){
    				_this.data = ret;
    				html = module.getModule({"name":_this.data.store.name},'storesInfoBrief')+
		            	   module.getModule({"list":[
								{"name":"商品数","key":_this.data.top.counts||0},
								{"name":"上架商品","key":_this.data.top.moneys||0},
								{"name":"仓库商品","key":_this.data.top.xiaoshou_price||0},
							]},'InfoTiled')+
		            	   _this.productManagerList(ret);
		            	   $('.ui-container').html(html);
	            });
    		break;
    		//店员管理
    		case "clerkManager": 
    			main.get(url.storeClerk,{"store_id":_this.store_id},function(ret){
    				_this.data = ret.data;
					html = module.getModule({'name':_this.data.name,"desc":"门店编号: "+_this.data.store_token},'storesInfoBrief')+
            		_this.clerkManagerList(_this.data);
            		$('.ui-container').html(html);
    			});
                //$(".ui-container").html("<a om-click='addClerkInfo'>123123</a>")
    		break;
    		//财务管理
    		case "finaceManager":
    			_this.getFinaceData();
    		break;
    		//门店管理
    		case "storeManager":
    			//main.get(url.storeClerk,{"store_id":_this.store_id},function(ret){
    				//_this.data = ret.data;
    				var data = {"list":[
						{"name":"门店","key":"10"},
						{"name":"累计粉丝","key":"10"},
						{"name":"累计销售额","key":"0"},
					]};
					var id = Math.ceil(Math.random()*1000);
					html = module.getModule(data,'InfoTiled')+
						   module.selectAddress(id,'om-change="getStoreList"')+
						   module.getModule('','arrlist');
            		$('.ui-container').html(html);
            		module.getSelectAddress($(".ui-form-item select[name='province']"),0);
            		//$('.goodsInfoTiled').after(module.selectAddress());
    			//});
    		break;
    	}
        //console.log(html);
    	
    }
    //添加店员申请
    this.addClerkInfo= function(ths){
        var data= {
            tType: "clerkInfo"
        }
        var option= {
            content: tplArr['manager'](data)
        }
        main.botActionsheet(option)
    }
    //下拉筛选
    this.selectFun = function(ths){
    	alert(ths.val());
    }
    //订单管理列表
    this.orderManagerList = function(ret){
        var data= {
    		"tType": "orderManagerList",
    		"list": ret,
    	}
        console.log(ret);
    	for(var n in data.list){
    		data.list[n].goodsInfo = _this.getGoodInfo(data.list[n].good);
    	}
    	return tplArr['manager'](data);
    }
    //商品管理列表
    this.clerkManagerList = function(ret){
    	var data= {
            "tType": "clerkManagerList",
            "list": ret.clerk
        }
        return tplArr['manager'](data);
    }
    //管理列表
    this.productManagerList = function(ret){
    	console.log(ret);
    	var data= {
            "tType": "productManagerList",
            "list": ret.data,
        }
        for(var n in data.list){
    		data.list[n].goodsInfo = module.ordersGoodsInfo([{
    			"title":data.list[n].product_name,
    			"image":data.list[n].base_image,
    			"colour":data.list[n].colour_array.join("、"),
    			"cup":data.list[n].cup_type_array.join("、"),
    			"size":data.list[n].size_type_array.join("、"),
    			"inStock":data.list[n].onCount+"",
    		}]);
    	}
        return tplArr['manager'](data);
    }
    //获取订单管理数据
    this.getOrderData = function(order_status){
    	if(!order_status){var order_status = ""}
    	main.get(url.orderlist,{"type":this.userType,"order_status":order_status},function(ret){
			var data = ret.data;
			_this.userStatus = data.role.status;
			html = module.getModule({"name":data.store_name,"status":data.role.status},'storesInfoBrief')+
        	   		module.getModule({"list":[
						{"name":"累计订单数","key":data.tongnji.danjia},
						{"name":"累计订单金额","key":data.tongnji.goods_amount},
						{"name":"课单价","key":data.tongnji.order_count},
					]},'InfoTiled')+
        	   		module.getModule({"placeholder":"搜索商品名称／商品编号／订单号"},'search')+
        	   		module.getModule({"fun":'om-change="selectOrderFun"',"list":[
        	   			{"name":"全部订单","value":""},
        	   			{"name":"待付款","value":"pay_0"},
        	   			{"name":"待收货","value":"shipping_1"},
        	   			{"name":"待评价","value":"assess_1"},
        	   			{"name":"退换货","value":"return_4"}
        	   			],"selected":_this.order_status},'baseSelect')+
        	   		_this.orderManagerList(data.data);
        	$('.ui-container').html(html);
		});
    }
    //订单类型筛选
    this.selectOrderFun = function(ths){
    	var val = ths.val();
    	_this.order_status = val;
    	_this.getOrderData(val);
    }
    //财务管理门店筛选
    this.selectFinaceStoreFun = function(ths){
    	var val = ths.val();
    	_this.store_id = val;
    	_this.getFinaceData(val);
    }
    //财务管理支付类型筛选
    this.tapFinaceFun = function(ths){
    	var duifu = ths.attr("name");
    	if(duifu == "3"){//跳转佣金排行榜

    	}else{
    		$('.ui-tab-nav').removeClass('current');
    		ths.addClass('current');
			_this.duifu = duifu;
			_this.getFinaceData(_this.store_id,duifu);
    	}
    }
    //获取财务管理数据
    this.getFinaceData = function(store_id,duifu){
    	if(!store_id){var store_id = ""}
    	if(!duifu){var duifu = ""}
    	main.get(url.Commission,{"store_id":store_id,"duifu":duifu},function(ret){
			_this.data = ret.data;
			var storeSelect = (function(o){ 
								var option = [{"name":"全部门店","value":""}];
								for(var n in o){
									option.push({"name":o[n].name,"value":o[n].store_id});
								}
								return option;
							})(_this.data.store); 
			html = module.getModule({"fun":'om-change="selectFinaceStoreFun"',"list":storeSelect,"selected":_this.store_id},'baseSelect')+
				   '<div class="finaceManagerCon"></div>';
    		$('.ui-container').html(html);
    		_this.finaceManagerHtml(_this.data);
		});
    }
    //财务列表
    this.finaceManagerHtml = function(ret){
    	if(!_this.duifu){_this.duifu = ""}
    	var tabNav = {
			"m": _this.duifu,
			"nav":{"全部佣金":"","已支付":"1","未支付":"2","佣金排行榜":"3"},
			"fun": 'om-click="tapFinaceFun"'
		}
    	var data= {
            "tType": "finaceManagerHtml",
            "tabNav": module.getModule(tabNav,'tabNav'),
            "InfoTiled": module.getModule(
	            	{"list":[
						{"name":"累计销售额","key":ret.top.xiaoshou_price||0},
						{"name":"累计佣金笔数","key":ret.top.counts||0},
						{"name":"累计佣金额","key":ret.top.moneys||0},
					]},'InfoTiled'),
            "list": ret.data,
        }
        for(var n in data.list){
    		data.list[n].goodsInfo = _this.getGoodInfo(data.list[n].good);
    	}
        $('.finaceManagerCon').html(tplArr['manager'](data));
    }
    //门店管理
    this.storeManagerHtml= function(ret){
    	console.log(ret);
    	var data= {
            "tType": "storeManagerHtml",
            //"list": data2
        }
        return tplArr['manager'](data);
    }
    //根据地址
    this.getStoreList = function(ths){
    	main.post(url.storeSingle,{"store_id":this.store_id},function(ret){
    		console.log("test");
    	});
    }

    this.getOrderList= function(data){ 
    	console.log(data);
    	var ret = data;
    	$.each(ret.dataList, function(i, v){  
    	 	var r= [];
    	 	r.push(v);
    		v.content= module.ordersGoodsInfo(r); 
    	})  
    	return ret;
    }
    //生成订单商品信息
    this.getGoodInfo = function(data){
        var good = [];
        for(var n =0;n<data.length;n++){
            var _json= data[n];
            var _data= {
                    "cart_id": _json.cart_id,
                    "id": _json.good_id,
                    "title":_json.good_name,
                    //"attr": [],
                    "num":_json.good_sku,
                    "spec":(_json.color||"")+" , "+(_json.cupou||"")+" , "+_json.size, 
                    "price":_json.market_price,
                    "image":_json.small_image,
                    "quantity":_json.good_number
            };
            good.push(_data);
        }  
        return module.ordersGoodsInfo(good);
    }
    this.init= function(){
    	this.store_id = main.GetQueryString('store_id')||"";
    	this.userType = main.GetQueryString('type');
    	this.order_status = main.GetQueryString('order_status')||"";
    	this.pageName = pageName;
    	this.createHtml();
    }
    return this.main();
};

var index = new Index();